<template>
  <div class="app-container search-list-container poll">
    <div class="search-wrapper">
      <el-form
        :model="queryParams"
        ref="queryForm"
        :inline="true"
        v-show="showSearch"
        label-width="auto"
      >
        <el-form-item label="创建时间" prop="expireDate">
          <el-date-picker
            size="small"
            v-model="constructionDateQuery"
            type="daterange"
            range-separator="至"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            placeholder="请选择创建时间"
          >
          </el-date-picker>
        </el-form-item>

        <el-form-item label="所属单位" prop="entitlementUnit">
          <el-cascader
            placeholder="请选择权属单位"
            clearable
            size="small"
            :show-all-levels="false"
            @change="monad"
            ref="cascaderRefs"
            v-model="messageList.deptId"
            :options="entitlementUnitToStringOptions"
            :props="{ value: 'id', label: 'label', checkStrictly: true }"
          ></el-cascader>
        </el-form-item>

        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form>
    </div>
    <div class="list-wrapper">
      <el-table v-loading="loading" :data="infoList" height="100%" width="100%">
        <el-table-column
          label="所属单位"
          align="center"
          prop="deptName"
          :render-header="renderHeader"
          show-overflow-tooltip
        />
        <el-table-column
          label="系统消息(条)"
          align="center"
          prop="systemMessage"
          :render-header="renderHeader"
          show-overflow-tooltip
        />

        <el-table-column
          label="通知公告(条)"
          align="center"
          prop="notifyBulletin"
          :render-header="renderHeader"
          show-overflow-tooltip
        >
        </el-table-column>
        <el-table-column
          label="告警消息"
          align="center"
          prop="alarmMessage"
          :render-header="renderHeader"
          show-overflow-tooltip
        >
        </el-table-column>
      </el-table>

      <pagination
        v-show="total > 0"
        :total="total"
        :page.sync="messageList.current"
        :limit.sync="messageList.size"
        @pagination="getList"
      />
    </div>
  </div>
</template>

<script>
import { listInfo, getInfo, pagIng } from "@/api/insideMessage/statistics";
import { getCompanyList } from "@/api/base/terminal/share/device"; //权属单位
export default {
  name: "Info",
  inject: ["reload"],
  components: {},
  data() {
    return {
      // 遮罩层
      loading: true,
      juBge: "",
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 终端鉴权白名单表格数据
      infoList: [],
      // 权属单位字典
      entitlementUnitToStringOptions: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 创建时间
      constructionDateQuery: [],
      // 查询参数
      queryParams: {
        current: 1,
        size: 10,
        messageTitle: null,
        messageType: null,
        expireDate: null,
        messageMethod: null,
      },
      messageList: {
        Date: "",
        startTime: "",
        endTime: "",
        deptId: "",
        current: 1,
        size: 10,
      },
      intIon: {
        current: 1,
        size: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        deviceId: [
          { required: true, message: "请输入终端编码", trigger: "blur" },
        ],
        macAddress: [
          { required: true, message: "请输入MAC地址", trigger: "blur" },
        ],
        ipAddress: [
          { required: true, message: "请输入IP地址", trigger: "blur" },
        ],
        expireDate: [
          {
            required: true,
            message: "选择过期时间",
            trigger: "change",
          },
        ],
      },
      openCheck: false,
    };
  },
  created() {
    this.getList();
    getCompanyList().then((res) => {
      let arr = [];
      arr.push(res.data[0]);
      this.entitlementUnitToStringOptions = arr;
    });
  },
  methods: {
    getList() {
      this.loading = true;
      if (
        this.messageList.entitlementUnit &&
        this.messageList.entitlementUnit.length > 0
      ) {
        this.messageList.entitlementUnit =
          this.messageList.entitlementUnit[
            this.messageList.entitlementUnit.length - 1
          ];
      }
      servIce(this.messageList).then((response) => {
        this.ttachment = response.data.records;
        this.loading = false;
      });
      transform(this.messageList).then((res) => {
        // console.log(res);
        this.facility.totalDevice = res.data.totalDevice;
        this.facility.totalOnlineDevice = res.data.totalOnlineDevice;
        this.facility.totalNormalDevice = res.data.totalNormalDevice;
        this.facility.totalAbnormalDevice = res.data.totalAbnormalDevice;
      });
    },
    // 权属单位自动收到下拉框
    monad() {
      this.$refs.cascaderRefs.dropDownVisible = false;
    },
    /** 查询终端鉴权白名单列表 */
    getList() {
      this.loading = true;
      if (this.constructionDateQuery.length) {
        this.messageList.startTime = `${this.constructionDateQuery[0]} 00:00:00`;
        this.messageList.endTime = `${this.constructionDateQuery[1]} 23:59:59`;
      }
      // 权属单位处理
      if (this.messageList.deptId && this.messageList.deptId.length > 0) {
        this.messageList.deptId =
          this.messageList.deptId[this.messageList.deptId.length - 1];
      }
      pagIng(this.messageList).then((res) => {
        this.total = res.data.total;
        this.infoList = res.data.records;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.messageList = {
        // idCode: null,
        // deivceName: null,
        // macAddress: null,
        // ipAddress: null,
        // expireDate: null,
        // entitlementUnit: null,
        // constructionDateQuery:null,
        Date: "",
        startTime: "",
        endTime: "",
        deptId: "",
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.messageList.current = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.constructionDateQuery = [];
      this.reset();
      this.resetForm("messageList");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.juBge = false;
      this.form = {};
      this.open = true;
      this.title = "新增消息";
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          addInfo(this.form).then((response) => {
            this.open = false;
            this.$modal.msgSuccess("新增成功");
            this.getList();
          });
        }
      });
    },
    closeAdd() {
      this.reload();
    },
    renderHeader(h, { column, $index }) {
      // 新建一个 span
      let span = document.createElement("span");
      // 设置表头名称
      span.innerText = column.label;
      // 临时插入 document
      document.body.appendChild(span);
      column.minWidth = span.getBoundingClientRect().width + 20;
      // 移除 document 中临时的 span
      document.body.removeChild(span);
      return h("span", column.label);
    },
  },
};
</script>
<style scoped lang="scss">
@import "~@/assets/styles/system.scss";
.poll ::v-deep .el-radio__label {
  color: #fff !important;
}
</style>
